﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace JzSport.Api
{
    /// <summary>
    /// 上传图片
    /// POST
    /// ./api/v1.1/action.ashx?sys:cmd=uploadimageresource
    /// form:
    ///     op:phone=???
    ///     &op:password=???
    ///     &op:verifycode=???
    /// </summary>
    class UploadImageResourceAction : AuthPostAction<Params.UploadImageResourceParams, Results.UploadImageResourceResult>
    {
        private static object s_lockDirObj = new object();

        protected override ActionResult<Results.UploadImageResourceResult> OnExecute(ActionContext context, SysParameters sysParameters, Params.UploadImageResourceParams opParams)
        {
            // 保存文件到文件目录
            string dir = HttpContext.Current.Server.MapPath("~/Resources/Images");
            if (!System.IO.Directory.Exists(dir))
            {
                lock (s_lockDirObj)
                {
                    if (!System.IO.Directory.Exists(dir))
                    {
                        System.IO.Directory.CreateDirectory(dir);
                        JizSport.Common.Logger.DebugToTag(LogTag.TAG, "UploadImageResourceAction create Directory:{0}", dir);
                    }
                }
            }

            string[] segments = opParams.File.FileName.Split(new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
            Guid imageId = Guid.NewGuid();
            string targetPath = string.Format("{0}/{1}.{2}", dir, imageId, segments.Last());
            opParams.File.SaveAs(targetPath);
            JizSport.Common.Logger.DebugToTag(LogTag.TAG, "UploadImageResourceAction upload file:{0} save to:{1}", opParams.File.FileName, targetPath);

            ImageResource imageResource = new ImageResource();
            imageResource.Id = imageId;
            imageResource.OwnerId = context.AccountId;
            imageResource.CreatedTime = DateTime.Now;
            imageResource.Usage = UsageCode.Activity;
            imageResource.Url = string.Format("./Resources/Images/{0}.{1}", imageId, segments.Last());

            ServiceResult<ImageResource> serviceResult = new ImageResourceService().Save(imageResource);

            Results.UploadImageResourceResult actionResult = new Results.UploadImageResourceResult();

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                actionResult.Id = imageResource.Id;
                actionResult.Url = string.Format("{0}{1}", Configs.BaseWebUrl, imageResource.Url);

                return new ActionResult<Results.UploadImageResourceResult>("1.0.2", true,
                    ErrorCode.NoError,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            else
            {
                return new ActionResult<Results.UploadImageResourceResult>("1.0.2", false,
                    serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
        }
    }
}
